package com.mmm.advert.controller;

import com.mmm.advert.entity.Advert;
import com.mmm.advert.service.AdvertService;
import com.mmm.advert.vo.Paging;
import com.mmm.common.vo.SuccessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Advert)表控制层
 *
 * @author One
 * @since 2022-02-14 22:55:26
 */
@RestController
@RequestMapping("/advert")
public class AdvertController {
    /**
     * 服务对象
     */
    @Autowired
    private AdvertService advertService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public SuccessVo<Advert> selectOne(@PathVariable Long id) {
        return new SuccessVo<>(this.advertService.queryById(id));
    }


    @DeleteMapping("/{id}")
    public SuccessVo<Object> deleteById(@PathVariable Long id) {

        return new SuccessVo<>(this.advertService.deleteById(id) ?2:0);
    }

    @PutMapping
    public SuccessVo<Advert> insert(@RequestBody Advert advert) {
        return  new SuccessVo<>(1,this.advertService.insert(advert));
    }

    @PostMapping
    public SuccessVo<Advert> update(@RequestBody Advert advert) {
        return  new SuccessVo<>(3,this.advertService.update(advert));
    }

    @GetMapping
    public SuccessVo<List<Advert>> getAll(){
        return  new SuccessVo<>(this.advertService.getAll());
    }

    @GetMapping("/list")
    public SuccessVo<Paging<Advert>> pageList(@RequestParam(defaultValue = "0") Integer page
            , @RequestParam(defaultValue = "10") Integer size){
        return  new SuccessVo<>(advertService.page(page,size));
    }
}